/**
 * 48. 旋转图像
 * https://leetcode-cn.com/problems/rotate-image/
 */
const { displayNestedArray } = require('../../libs/public');

/**
 * @param {number[][]} matrix
 * @return {void} Do not return anything, modify matrix in-place instead.
 */
function rotate(matrix) {
  const { length } = matrix;
  const m = Math.floor(length / 2);
  const n = length - m;
  for (let i = 0; i < m; i += 1) {
    for (let j = 0; j < n; j += 1) {
      const temp = matrix[i][j];
      matrix[i][j] = matrix[length - j - 1][i];
      matrix[length - j - 1][i] = matrix[length - i - 1][length - j - 1];
      matrix[length - i - 1][length - j - 1] = matrix[j][length - i - 1];
      matrix[j][length - i - 1] = temp;
    }
  }
}

let matrix;

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
rotate(matrix);
console.log(displayNestedArray(matrix) === '[[7,4,1],[8,5,2],[9,6,3]]');
matrix = [[5, 1, 9, 11], [2, 4, 8, 10], [13, 3, 6, 7], [15, 14, 12, 16]];
rotate(matrix);
console.log(displayNestedArray(matrix) === '[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]');
